package com.manage.modules.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.manage.modules.bo.TBaseEquipTypeAddBo;
import com.manage.modules.bo.TBaseEquipTypeQueryBo;
import com.manage.modules.bo.TBaseEquipTypeEditBo;
import com.manage.modules.domain.TBaseEquipType;
import com.manage.modules.mapper.TBaseEquipTypeMapper;
import com.manage.modules.vo.TBaseEquipTypeVo;
import com.manage.modules.service.ITBaseEquipTypeService;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 设备类型Service业务层处理
 *
 * @author wx
 * @date 2021-08-12
 */
@Service
public class TBaseEquipTypeServiceImpl extends ServiceImpl<TBaseEquipTypeMapper, TBaseEquipType> implements ITBaseEquipTypeService {

    @Override
    public TBaseEquipTypeVo queryById(Long equipTypeId) {
        TBaseEquipType db = this.baseMapper.selectById(equipTypeId);
        return BeanUtil.toBean(db, TBaseEquipTypeVo.class);
    }

    @Override
    public List<TBaseEquipTypeVo> queryList(TBaseEquipTypeQueryBo bo) {
        LambdaQueryWrapper<TBaseEquipType> lqw = Wrappers.lambdaQuery();
        lqw.like(StrUtil.isNotBlank(bo.getEquipTypeName()), TBaseEquipType::getEquipTypeName, bo.getEquipTypeName());
        lqw.eq(bo.getEquipTypeState() != null, TBaseEquipType::getEquipTypeState, bo.getEquipTypeState());
        return entity2Vo(this.list(lqw));
    }

    /**
     * 实体类转化成视图对象
     *
     * @param collection 实体类集合
     * @return
     */
    private List<TBaseEquipTypeVo> entity2Vo(Collection<TBaseEquipType> collection) {
        List<TBaseEquipTypeVo> voList = collection.stream()
                .map(any -> BeanUtil.toBean(any, TBaseEquipTypeVo.class))
                .collect(Collectors.toList());
        if (collection instanceof Page) {
            Page<TBaseEquipType> page = (Page<TBaseEquipType>) collection;
            Page<TBaseEquipTypeVo> pageVo = new Page<>();
            BeanUtil.copyProperties(page, pageVo);
            pageVo.addAll(voList);
            voList = pageVo;
        }
        return voList;
    }

    @Override
    public Boolean insertByAddBo(TBaseEquipTypeAddBo bo) {
        TBaseEquipType add = BeanUtil.toBean(bo, TBaseEquipType.class);
        validEntityBeforeSave(add);
        return this.save(add);
    }

    @Override
    public Boolean updateByEditBo(TBaseEquipTypeEditBo bo) {
        TBaseEquipType update = BeanUtil.toBean(bo, TBaseEquipType.class);
        validEntityBeforeSave(update);
        return this.updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(TBaseEquipType entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return this.removeByIds(ids);
    }
}
